import 'package:m_todo/domain/entity/todo_entity.dart';
import 'package:m_todo/domain/store/base_todo_store.dart';

/// todoList 核心服务
class TodoService {
  final BaseTodoStore _store;

  TodoService(this._store);

  Future<TodoEntity> add(String content) async {
    TodoEntity todoEntity = TodoEntity(content: content, state: TodoStateConst.unDone);
    var index = await _store.add(todoEntity);
    todoEntity.index = index;
    return todoEntity;
  }

  Future<void> done(int index) async {
    TodoEntity? todo = await _store.query(index);
    if (todo == null) {
      return Future.error('Todo with index $index was not found');
    }
    todo.state = TodoStateConst.done;
    _store.update(todo);
  }

  Future<List<TodoEntity>> getUnDoneList() {
    return _store.queryUnDoneList();
  }

  Future<List<TodoEntity>> getAllList() {
    return _store.queryAll();
  }

  Future<String> export(String targetPath) {
    return _store.export(targetPath);
  }

  Future<int> import(String targetFilePath) {
    return _store.import(targetFilePath);
  }
}
